<include file="public@header_vue"/>
</head>
<style>
    .el-pagination {
        margin-top: 20px;
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .photos {
        width: 120px;
        height: 120px;
        border-radius: 6px;
        overflow: hidden;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
    }

    .photos-options {
        position: absolute;
        top: 0;
        left: 0;
        height: 120px;
        line-height: 120px;
        width: 120px;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
        z-index: 10;
        opacity: 0;
        transition: opacity 0.6s;
    }

    .photos:hover .photos-options {
        opacity: 1;
    }

    .photos-options .el-button {
        font-size: 24px;
        color: white
    }

    .photos img {
        width: 120px;
        height: 120px;
    }

    .photos-uploader {
        width: 120px;
        height: 120px;
        float: left;
    }

    .logo-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .logo-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
</style>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="分类管理" v-loading="onDataLoading">
            <el-row>
                <el-col :span="24">
                    <el-form :inline="true" :model="form" size="mini">
                        <el-form-item label="日期:">
                            <el-date-picker v-model="form.duration" type="daterange" value-format="yyyy-MM-dd"
                                            :picker-options="durationOptions" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                        </el-form-item>
                        <el-form-item v-if="{$role_id} != 3" label="公司：">
                            <el-select v-model="form.company_id" placeholder="选择公司">
                                <el-option
                                        v-for="item in companyData"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="getData()">查询</el-button>
                            <el-button type="danger" @click="clearForm()">清空</el-button>
                            <el-button type="primary" @click="update(null)">新增分类</el-button>
                            <el-button type="primary" v-if="{$role_id} != 3" @click="copy(null)">复制分类</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="sort" label="排序" width="80">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.sort"
                                  @change="change_status('sort',scope.row.sort,scope.row.id)"></el-input>
                    </template>
                </el-table-column>
                <el-table-column v-if="{$role_id} != 3" prop="company_info.name" label="公司名称"
                ></el-table-column>
                <el-table-column prop="name" label="分类名称"></el-table-column>
                <el-table-column prop="status" label="状态">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.status"
                                active-color="#13ce66"
                                :active-value="1"
                                :inactive-value="0"
                                @change="change_status('status',scope.row.status,scope.row.id)"
                                inactive-color="#ff4949">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="update(scope.row)">编辑</el-button>
                        <el-button size="mini" type="text" @click="delete_activity(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-tab-pane>
        <el-dialog title="复制分类" :visible.sync="dialogCopyVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="categoryForm" class="demo-form-inline" :rules="category_rules" ref="categoryForm"
                     label-width="200px">
                <el-form-item label="选择复制公司" prop="old_company_id">
                    <el-col :span="24">
                        <el-select v-model="categoryForm.old_company_id" placeholder="选择复制公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="选择目标公司" prop="new_company_id">
                    <el-col :span="24">
                        <el-select v-model="categoryForm.new_company_id" placeholder="选择目标公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submit('categoryForm')">复制</el-button>
        </span>
        </el-dialog>
        <el-dialog :title="goodsForm.id == '' ? '新增分类信息' : '修改分类信息'" :visible.sync="dialogVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="goodsForm" class="demo-form-inline" :rules="rules" ref="goodsForm"
                     label-width="200px">
                <el-form-item label="分类名称" prop="name">
                    <el-col :span="24">
                        <el-input v-model="goodsForm.name" placeholder="分类名称"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="{$role_id}!=3" label="选择公司" prop="company_id">
                    <el-col :span="24">
                        <el-select v-model="goodsForm.company_id" placeholder="选择公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="goodsForm.id === ''" type="primary" @click="submit('goodsForm')">新增</el-button>
            <el-button v-else type="primary" @click="submit('goodsForm')">修改</el-button>
        </span>
        </el-dialog>
    </el-tabs>
</div>
</body>
<script>
    var app = new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                companyData: [],
                form: {
                    // name: '',
                    company_id: '',
                    duration: '',
                },
                categoryForm: {
                    old_company_id: '',
                    new_company_id: ''
                },
                dialogCopyVisible: false,
                myConfig: {
                    // 编辑器不自动被内容撑高
                    autoHeightEnabled: false,
                    // 初始容器高度
                    initialFrameHeight: 400,
                    zIndex: 3000,
                    // 初始容器宽度
                    initialFrameWidth: '90%',
                    // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                    serverUrl: 'http://fangtai.0513app.cn/static/UEditor/php/controller.php',
                    // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
                    UEDITOR_HOME_URL: '/static/UEditor/',
                },
                buy_type: [
                    {id: 0, name: '全现金'},
                    {id: 1, name: '全积分'},
                    {id: 2, name: '现金+积分'}
                ],
                totalNum: 0,
                currentPage: 1,
                dialogLoading: false,
                onDataLoading: false,
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                goodsForm: {
                    name: ''
                },
                dialogVisible: false,
                rules: {
                    name: [{required: true, message: '请输入分类名称', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                },
                category_rules:{
                    old_company_id: [{required: true, message: '请选择复制公司', trigger: 'blur'}],
                    new_company_id: [{required: true, message: '请选择目标公司', trigger: 'blur'}],
                },
                headers: {'X-Requested-With': 'XMLHttpRequest'},
            }
        },
        created() {
            this.getCompanyData();
            this.getData()
        },
        methods: {
            copy() {
                this.dialogCopyVisible = true;
            },
            clearForm() {
                this.form = {
                    company_id: '',
                    duration: '',
                }
            },
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                if (this.form.company_id) {
                    params.company_id = this.form.company_id;
                }
                axios.get("{:url('admin/goods/category')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data;

                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.sourceFrom.cover_img = res.url;
                console.log(this.sourceFrom.cover_img)
            },
            handleRemove(file, fileList) {
                let index = this.goodsForm.goods_img.indexOf(file);
                this.goodsForm.goods_img.splice(index, 1);
            },
            handleintroimgSuccess(res, file) {
                if (res.code === 1) {
                    this.goodsForm.goods_img.push(res.url);
                } else {
                    this.$message.error(res.msg)
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || 'image/png';

                const isLt2M = file.size / 1024 / 1024 < 10;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式或者PNG格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            change_status(field, value, id) {
                let _this = this;
                this.dialogLoading = true;
                let params = {field: field, value: value, id: id};
                axios.post("{:url('admin/goods/change_category_status')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            update(data) {
                let _this = this;
                if (data) {
                    _this.goodsForm = JSON.parse(JSON.stringify(data));
                    console.log(_this.goodsForm)
                } else {
                    _this.goodsForm = {
                        id: '',
                        name: '',
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },
            submit(form) {
                let _this = this;
                if (form == 'categoryForm') {
                    _this.$refs[form].validate((valid) => {
                        if (valid) {
                            axios.post("{:url('admin/goods/category_copy')}", this.categoryForm, {
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest',
                                }
                            }).then(function (response) {
                                _this.dialogLoading = false;
                                if (response.data.code === 1) {
                                    _this.$message.success('复制成功');
                                    _this.getData();
                                    _this.dialogVisible = false;
                                } else {
                                    _this.$message.warning(response.data.msg);
                                }
                            }).catch(function (error) {
                                _this.dialogLoading = false;
                                _this.$message.error(error.toString())
                            })
                        } else {
                            _this.dialogLoading = false;
                            return false;
                        }
                    });
                } else if (form == 'goodsForm') {
                    _this.$refs[form].validate((valid) => {
                        if (valid) {
                            axios.post("{:url('admin/goods/category_update')}", this.goodsForm, {
                                headers: {
                                    'X-Requested-With': 'XMLHttpRequest',
                                }
                            }).then(function (response) {
                                _this.dialogLoading = false;
                                if (response.data.code === 1) {
                                    if (_this.goodsForm.id === '') {
                                        _this.$message.success('新增完成');
                                    } else {
                                        _this.$message.success('修改成功');
                                    }
                                    _this.getData();
                                    _this.dialogVisible = false;
                                } else {
                                    _this.$message.warning(response.data.msg);
                                }
                            }).catch(function (error) {
                                _this.dialogLoading = false;
                                _this.$message.error(error.toString())
                            })
                        } else {
                            _this.dialogLoading = false;
                            return false;
                        }
                    });
                }

            },
            add_menu() {
                this.goodsForm.goods_specs.push({
                    value: '',
                    key: Date.now()
                });
            },
            remove_menu(item) {
                var index = this.goodsForm.goods_specs.indexOf(item)
                if (index !== -1) {
                    this.goodsForm.goods_specs.splice(index, 1)
                }
            },
            delete_activity(id) {
                let _this = this;
                axios.post("{:url('admin/goods/delete')}", {id: id}, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('删除成功');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                }).catch(function (error) {
                    _this.dialogLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            formatDate(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp * 1000);
                    return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
                } else {
                    return '';
                }
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        },

    });
</script>
